package stack_queue;

/**
 * 实现自己的栈
 * 元素类型为 long
 * 使用数组(顺序表)实现栈
 * 使用尾插：压栈
 * 使用尾删：弹栈
 * 不考虑扩容
 */

public class Stack {
    private final long[] array = new long[100];
    private int size = 0;
    public int size(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    public void push(long e){
        array[size++] = e;
    }

    public long pop(){
        if(isEmpty()){
            throw new RuntimeException("栈为空，无法弹栈");
        }
        return array[--size];//返回下标为 size 的元素，并且将 size - 1
    }

    public long peek(){
        if(isEmpty()){
            throw new RuntimeException("栈为空，无法查看");
        }
        return array[size - 1];
    }

    public static void main(String[] args) {
        Stack stack = new Stack();
        stack.push(1);
        stack.push(8);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.peek());       //4
        stack.push(7);
        System.out.println(stack.pop());        //7
        System.out.println(stack.peek());       //4
        stack.pop();
        System.out.println(stack.peek());       //3
        System.out.println(stack.size);         //3
    }
}
